package com.woniu.microassetserver.mapper;

import com.woniu.microcommonsserver.entity.AssetsParameterEntity;
import com.woniu.microcommonsserver.entity.DomaintypeEntity;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;

@Mapper
public interface DomaintypeMapper {

    /**
     * 根据参数进行分页查询
     * @param domaintypeEntity
     * @return
     * @author luoyue
     */
    List<DomaintypeEntity> findAllByParams(@Param("dm") DomaintypeEntity domaintypeEntity);



    @Select("select id,domain_name,description,domain_code," +
            "father_id from domaintype where  father_id=0 ")
    @Results({

            @Result(id = true, property = "id", column = "id", javaType = Long.class),
            @Result(property="domainName",column="domain_name", javaType = String.class),
            @Result(property="description",column="description", javaType = String.class),
            @Result(property="domainCode",column="domain_code", javaType = String.class),
            @Result(property="fatherId",column="father_id", javaType = Long.class),


            @Result(column="id",property="domaintypeEntity",javaType = List.class,
                    many=@Many(
                            select="com.woniu.microassetserver.mapper.DomaintypeMapper.findOther",fetchType = FetchType.EAGER
                    ))
    })
    List<DomaintypeEntity> findAll();





    @Select("select id,asset_name,fk_device_system_id,asset_information" +
            " from assets_parameter where  fk_device_system_id=#{id} ")
    @Results({

            @Result(id = true, property = "id", column = "id", javaType = Long.class),
            @Result(property = "assetName", column = "asset_name", javaType = String.class),
            @Result(property = "fkDeviceSystemId", column = "fk_device_system_id", javaType = int.class),
            @Result(property = "assetInformation", column = "asset_information", javaType = String.class),
    })
    List<AssetsParameterEntity> findAssetById(Long id);



    @Select("select id,domain_name,description,domain_code," +
            "father_id from domaintype where father_id=#{id}")
    @Results({
            @Result(id = true, property = "id", column = "id", javaType = Long.class),
            @Result(property="domainName",column="domain_name", javaType = String.class),
            @Result(property="description",column="description", javaType = String.class),
            @Result(property="domainCode",column="domain_code", javaType = String.class),
            @Result(property="fatherId",column="father_id", javaType = Long.class),

    })
    List<DomaintypeEntity> findOther(Long id);







    /**
     * 新增领域类型
     * @param domaintypeEntity
     * @return 受影响的行数
     * @author luoyue
     */
    @Options(
            useGeneratedKeys = true,
            keyProperty = "dm.id"
    )
    @Insert("insert into domaintype " +
            "(domain_name,description,domain_code,father_id)" +
            " values(#{dm.domainName},#{dm.description},#{dm.domainCode},#{dm.fatherId})")
    int saveDomaintype(@Param("dm") DomaintypeEntity domaintypeEntity);



    /**
     * 根据ID查询设备类型
     *
     * @param id
     * @return
     * @author luoyue
     */
    @Results({



            @Result(id = true, property = "id", column = "id", javaType = Long.class),
            @Result(property="domainName",column="domain_name", javaType = String.class),
            @Result(property="description",column="description", javaType = String.class),
            @Result(property="domainCode",column="domain_code", javaType = String.class),
            @Result(property="fatherId",column="father_id", javaType = Long.class),

            /*@Result(property = "customerInfo", column = "fk_customer_id", javaType = AssetsParameterBean.class, one =
            @One(select = "com.woniuxy.creditsystem.common.mapper.CustomerMapper.getOneById", fetchType = FetchType.LAZY)),*/
    })
    @Select("select * from domaintype where id = #{id}")
    DomaintypeEntity getOneById(Long id);





    /**
     * 修改领域类型
     * @param domaintypeEntity
     * @return 受影响的行数
     */
    @Update("<script>" +
            "update domaintype" +
            "<set>" +
            "<if test='dm.domainName != null'>" +
            "domain_name = #{dm.domainName}," +
            "</if>" +
            "</set>" +
            "where id = #{ap.id}" +
            "</script>")
    int updateDomaintype(@Param("dm") DomaintypeEntity domaintypeEntity);


    /**
     * 删除领域类型
     * @param id
     * @return 受影响的行数
     */

    @Delete("delete from domaintype where id = #{id}")
    int deleteDomaintype(@Param("id")Long id);





}
